package com.yiren.dbaa.module.device.presenter;

import android.app.Activity;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.dbaa.constant.G;
import com.yiren.dbaa.currency.model.ObdCarModel;
import com.yiren.dbaa.module.device.contract.DeviceConnectContract;
import com.yiren.dbaa.module.device.contract.LowerComputerContract;
import com.yiren.dbaa.module.device.model.DeviceConnectModel;
import com.yiren.dbaa.module.device.model.LowerComputerModel;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * Created by WANGRONGXIANG
 * on 2022/6/21
 * Describe
 */
public class LowerComputerPresenter implements LowerComputerContract.Presenter {

    private Activity activity;
    private LowerComputerContract.Model lowerComputerModel;
    private LowerComputerContract.View lowerComputerView;
    private DeviceConnectContract.Model deviceConnectModel;

    private boolean lowerMachineDataOK = false; //下位机是否获取到数据
    private int switchNumber = 0;               //切换车辆模型的次数

    public LowerComputerPresenter(Activity activity) {
        this.activity = activity;
        lowerComputerModel = new LowerComputerModel(activity);
        deviceConnectModel = new DeviceConnectModel(activity);
    }

    @Override
    public void setObdCarModelCode() {
        Integer obdCarModelCode = LoadConfigApi.loadSignalSourceConfig(activity).getObdModel();
        if (null != G.connectDeviceMessage && G.connectDeviceMessage.getVersionDescribe().contains("JX-YR")) {
            //如果是自研主机接线板，则车辆类型为0
            if (obdCarModelCode != 0) {
                lowerComputerModel.saveCarModel(0);
            }
        } else {
            String str = SignalGConstant.carMasterDataMP;
            if (StringUtils.isNotEmpty(str)) {
                String[] split = str.split(";");
                int obdCarCode = Integer.parseInt(split[1]);
                if (null == obdCarModelCode || obdCarModelCode != obdCarCode) {
                    lowerComputerModel.saveCarModel(obdCarCode);
                }
            }
        }
    }

    @Override
    public String getObdCarModelName() {
        return lowerComputerModel.getObdName();
    }

    @Override
    public void switchObdCarModel(LowerComputerContract.View view) {
        this.lowerComputerView = view;
        List<ObdCarModel> obdCarModels = lowerComputerModel.getObdCarModels();
        view.showSelectObdCarModelDialog(obdCarModels);
    }

    @Override
    public void switchObdCarModel(int obdCarModelCode) {
        lowerMachineDataOK = false;
        while (!lowerMachineDataOK) {
            // 给下位机发送车型数据
            lowerComputerModel.switchCarModel(obdCarModelCode);
            // 做一个延时效果,给主机一个切换的时间
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                System.out.println(e.toString());
            }
            // 判断是否重连三次，超过三次需要提示客户选择车型失败，重新选择
            if (switchNumber >= 2) {
                LoadDialog.dismiss(activity);
                MessageBox.show(activity, "选择车型失败，请重新选择");
                lowerMachineDataOK = true;
                break;
            }
            switchNumber++;
            //保存车辆模型
            // 如果发送OBD类型后产生了正确的变化就停止继续发送
            if (obdCarModelCode == 0) {
                LoadDialog.dismiss(activity);
                lowerMachineDataOK = true;
                lowerComputerModel.saveCarModel(obdCarModelCode);
                MessageBox.show(activity, "选择车型成功");
                lowerComputerView.selectCarModelSuccess(getObdCarModelName());
                if (null != G.connectDeviceMessage && (G.connectDeviceMessage.getVersionDescribe().contains("YR") || G.connectDeviceMessage.getVersionDescribe().contains("XQ") || G.connectDeviceMessage.getVersionDescribe().contains("WP"))) {
                    //自研主机盒可以切换接线板，所以需要重新获取主机盒版本号信息
                    deviceConnectModel.connectDevice(new DeviceConnectContract.DeviceConnectCallback() {
                        @Override
                        public void onDeviceConnectSuccess() {
                            lowerComputerView.updateDeviceInfo();
                        }
                    });
                }
                break;
            } else {
                String strMP = SignalGConstant.carMasterDataMP;
                if (null != strMP && !"".equals(strMP)) {
                    String[] split = strMP.split(";");
                    if (Integer.parseInt(split[1]) == obdCarModelCode) {
                        LoadDialog.dismiss(activity);
                        lowerMachineDataOK = true;
                        lowerComputerModel.saveCarModel(obdCarModelCode);
                        lowerComputerView.selectCarModelSuccess(getObdCarModelName());
                        break;
                    }
                }
            }
        }
    }
}
